package com.lyl.goods.controller;

import com.github.pagehelper.PageInfo;
import com.lyl.goods.pojo.Product;
import com.lyl.goods.service.ProductService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


import java.util.List;


@Controller
public class ProductController {
    @Autowired
    private ProductService productService;


    /***
     * @Description: 商品展示
     * @Author: liyunlong
     * @Date: 2024/3/1
     */
    @GetMapping("/product")
    public String getProductList(@RequestParam(defaultValue = "1") int pageNum,
                                 @RequestParam(defaultValue = "10") int pageSize, HttpSession session, Model model) {
        String currentUser = (String) session.getAttribute("currentUser");
        model.addAttribute("userName", currentUser);
        // 根据pageNum和pageSize查询数据库获取相应分页的商品数据
        List<Product> productList = productService.getProductList(pageNum, pageSize);
        int totalRecords = productService.getTotalCount(); // 获取总记录数
        int totalPages = (int) Math.ceil((double) totalRecords / pageSize); // 计算总页数
        int maxVisiblePages = 5; // 最大显示页码数
        // 将分页后的商品数据和分页信息添加到模型中
        model.addAttribute("productList", productList);
        model.addAttribute("maxVisiblePages", maxVisiblePages);
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("pages", totalPages);
        model.addAttribute("total", totalRecords);
        return "product/product";
    }

    /**
     * @Description: 添加商品
     * @Author: liyunlong
     * @Date: 2024/3/1
     */
    @GetMapping("/addProduct")
    public String showRForm() {
        return "/addProduct";
    }

    @RequestMapping(value = "/addProduct", method = RequestMethod.POST)
    public String insert(@RequestParam("name") String name,
                         @RequestParam("price") Integer price,
                         @RequestParam("stock") Integer stock,
                         @RequestParam("description") String description) {
        productService.add(name, price, stock, description);
        return "redirect:/product";
    }

    /**
     * @Description: 修改商品
     * @Author: liyunlong
     * @Date: 2024/3/1
     */
    @GetMapping("/updateProduct")
    public String showUpdateForm(@RequestParam("id") Long id, Model model) {
        Product product = productService.getProductById(id);
        model.addAttribute("product", product);
        return "updateProduct"; // 返回更新商品页面
    }

    // 处理修改商品请求
    @PostMapping("/updateProduct")
    public String updateProduct(@ModelAttribute Product product) {
        productService.updateProduct(product);
        return "redirect:/product"; // 更新成功后重定向到商品列表页面
    }

    /**
     * @Description: 商品详情
     * @Author: Liyunlong
     * @Date: 2024/3/1
     */
    @GetMapping("/detailProduct")
    public String showDetailForm(@RequestParam("id") Long id, Model model) {
        Product product = productService.getProductById(id);
        model.addAttribute("product", product);
        return "detailProduct";
    }


    /**
     * @Description: 商品详情
     * @Author: liyunlong
     * @Date: 2024/3/1
     */
    @GetMapping("/deleteProduct")
    public String showDeleteForm(@RequestParam("id") Long id, Model model) {
        Product product = productService.getProductById(id);
        model.addAttribute("product", product);
        return "deleteProduct"; // 返回更新商品页面
    }

    @RequestMapping("/deleteProduct")
    public String DeleteForm(@RequestParam("id") Long id) {
        productService.delete(id);
        return "redirect:/product"; // 更新成功后重定向到商品列表页面
    }

    /**
     * @Description: 批量删除
     * @Author: liyunlong
     * @Date: 2024/3/1
     */

    //批量删除商品
    @PostMapping("/batchDeleteProduct")
    public String batchDeleteProduct(@RequestParam("selectedProductIds") List<Integer> selectedProductIds) {
        try {
            productService.batchDeleteProducts(selectedProductIds);
            return "redirect:/product"; // 重定向到商品列表页面
        } catch (Exception e) {
            // 处理异常情况
            e.printStackTrace();
            return "errorPage"; // 返回错误页面或者其他处理方式
        }
    }
}
